package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;


public class CompanyServiceImpl implements CompanyService {
    private CompanyDao companyDao = new CompanyImpl();

    @Override
    public CompanyModel getCompanyModel() {
//        返回的数据
        CompanyModel companyModel = new CompanyModel();
//        临时变量存储数据:现在用redis
//        Object data = null;
        Jedis jedis = new Jedis();
//        是否第一次访问,想要判断是否第一次，可以直接判断redis里面是否有数据
//        如果redis里面有数据，则不是第一次，如果没有数据，则表示第一次访问
        String logo = jedis.get("logo");

        boolean isFirst = logo == null;
        if (isFirst) {
//            如果第一次访问，则将数据从数据库取出来，放入到companyModel
            companyModel = companyDao.getCompanyModel();
//            同时将companyModel放入到临时变量
//            data = companyModel;
            //            如果不是第一次进来，则直接拿到临时变量的数据，并将数据保存到companyModel；
//            companyModel = data;
            //            jedis.set("companyModel",companyModel.toString());
//            jedis.set("logo", companyModel.getLogo());
////            设定一个过期时间
//            jedis.expire("logo",60*30);
            jedis.setex("logo",60*30,companyModel.getLogo());
            jedis.set("phone", companyModel.getPhone());
            jedis.set("net", companyModel.getNet());
            jedis.set("email", companyModel.getEmail());
            jedis.set("address", companyModel.getAddress());
            jedis.set("linePhone", companyModel.getLinePhone());
            jedis.set("version", companyModel.getVersion());

            System.out.println("mysql");

        } else {
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setAddress(jedis.get("address"));
            companyModel.setLinePhone(jedis.get("linePhone"));
            companyModel.setVersion(jedis.get("version"));
            System.out.println("redis");
        }
//        返回最终数据结果
        return companyModel;

    }
}
